#!/bin/bash

set -e

KEYWORDS="-k__ -k__n -k__x -k__d -k__d -k__s -k__sx"

SRC=../../main

FILES=`find $SRC -name *.pm -o -name *.cgi -o -name restore-tool`
rm -rf templates/*
for i in `find $SRC -name *.mas`
do
    dir=`dirname $i | sed 's:../../::'`
    mkdir -p templates/$dir
    tools/extract-perl-from-mason dump $i > templates/$dir/`basename $i`
done
TEMPLATE_FILES=`find templates -name *.mas`

function extract_strings
{
    dir=$1
    domain=$2
    shift; shift
    kwords=$@
    xgettext --default-domain=$domain \
             --add-comments=TRANSLATORS: --language Perl $kwords --flag=__:1:pass-perl-format --flag=__:1:pass-perl-brace-format \
             --omit-header --from-code=utf-8 \
             --output=$domain.po \
             $FILES $TEMPLATE_FILES

    sed -i 's|^#: templates/main|#: ../../main|g' $domain.po

    for i in $dir/*.po
    do
        echo "Updating $i..."
        msgmerge $i $domain.po -o $i.new
        mv $i.new $i
    done

    rm -f $domain.po
}

extract_strings po zentyal $KEYWORDS

rm -rf templates
